package org.lc.stk.dao.baseInfo;

import java.util.List;
import java.util.Optional;

import org.lc.stk.model.baseInfo.Department;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface DepartmentRepository extends JpaRepository<Department, Integer>, JpaSpecificationExecutor<Department> {
    
    Optional<Department> findByCode(String code);
    
    Optional<Department> findByName(String name);
    
    List<Department> findByParentIsNull();
    
    List<Department> findByParent(Department parent);
    
    @Query("SELECT d FROM Department d WHERE d.isDeleted = false")
    List<Department> findAllActive();
    
    @Query("SELECT d FROM Department d WHERE d.parent.id = :parentId AND d.isDeleted = false")
    List<Department> findByParentId(Integer parentId);
}